﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using ManagerDB;

namespace MMDescEditor
{
    /// <summary>
    /// Saves file into XML.
    /// </summary>
    class DescriptionFileWriter
    {
        StoredFile mFile;
        XmlDocument mXml = new XmlDocument();
        public DescriptionFileWriter(StoredFile file)
        {
            mFile = file;
        }

        public void Save(string filePath)
        {
            var descNode = mXml.CreateElement("desc");
            saveDate(descNode);
            saveName(descNode);
            saveOriginalName(descNode);
            saveDescription(descNode);
            saveFiles(descNode);
            saveImages(descNode);
            mXml.AppendChild(descNode);
            mXml.Save(filePath);
        }

        /// <summary>
        /// Saves files.
        /// </summary>
        /// <param name="node"></param>
        private void saveFiles(XmlNode node)
        {
            if (mFile.Files.Count == 0)
                return;

            var filesNode = mXml.CreateElement("files");
            foreach (FileDescription fileDesc in mFile.Files)
            {
                var fileNode = mXml.CreateElement("file");

                var tthAttr = mXml.CreateAttribute("TTH");
                tthAttr.InnerText = fileDesc.ID.TTH;
                fileNode.Attributes.Append(tthAttr);

                var sizeAttr = mXml.CreateAttribute("size");
                sizeAttr.InnerText = fileDesc.ID.Size.ToString();
                fileNode.Attributes.Append(sizeAttr);

                fileNode.InnerText = fileDesc.TechInfo;

                filesNode.AppendChild(fileNode);
            }
            node.AppendChild(filesNode);
        }

        private void saveImages(XmlNode node)
        {
            if (mFile.Images.Count == 0)
                return;

            var filesNode = mXml.CreateElement("covers");
            foreach (FileID fileId in mFile.Images)
            {
                var fileNode = mXml.CreateElement("cover");

                var tthAttr = mXml.CreateAttribute("TTH");
                tthAttr.InnerText = fileId.TTH;
                fileNode.Attributes.Append(tthAttr);

                var sizeAttr = mXml.CreateAttribute("size");
                sizeAttr.InnerText = fileId.Size.ToString();
                fileNode.Attributes.Append(sizeAttr);

                filesNode.AppendChild(fileNode);
            }
            node.AppendChild(filesNode);
        }

        /// <summary>
        /// Saves description.
        /// </summary>
        /// <param name="node"></param>
        private void saveDescription(XmlNode node)
        {
            if (mFile.Description == null)
                return;

            var descNode = mXml.CreateElement("description");
            descNode.InnerText = mFile.Description;
            node.AppendChild(descNode);
        }

        /// <summary>
        /// Saves original name.
        /// </summary>
        /// <param name="node"></param>
        private void saveOriginalName(XmlNode node)
        {
            if (mFile.Original == null)
                return;

            var nameNode = mXml.CreateElement("original_name");
            nameNode.InnerText = mFile.Original;
            node.AppendChild(nameNode);
        }

        /// <summary>
        /// Saves name.
        /// </summary>
        /// <param name="node"></param>
        private void saveName(XmlNode node)
        {
            if (mFile.Title == null)
                return;

            var nameNode = mXml.CreateElement("name");
            nameNode.InnerText = mFile.Title;
            node.AppendChild(nameNode);
        }

        /// <summary>
        /// Saves year/month/day.
        /// </summary>
        /// <param name="node"></param>
        private void saveDate(XmlNode node)
        {
            if (mFile.Year == 0)
                return;

            var dateNode = mXml.CreateElement("date");
            DateTime dt = new DateTime((int)mFile.Year, (int)mFile.Month, (int)mFile.Day);
            dateNode.InnerText = dt.ToShortDateString();
            node.AppendChild(dateNode);
        }
    }
}
